home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-04-14 | 8.6 KB | 345 lines | [TEXT/KAHL] |
- /*
- File SCSICmds.c, include file for SCSI.c
- Leo Drizis, March 1989
- Only for personal use, commercial use prohibited !!!
-
- Contains all the basic SCSI Commands
- */
-
- read(lblock,buffPtr,len) /* reads a sector from the disk */
- long lblock; /* sector number */
- unsigned int len; /* length of sector in bytes */
- char *buffPtr; /* pointer to data */
- {
- reWrCmd reCmd;
-
- reCmd.opCode=0x8; /* read command opcode */
- reCmd.rsrv1=lblock>>16;
- reCmd.addr=lblock&0xFFFF;
- reCmd.length=1; /* read one sector */
- reCmd.rsrv2=0;
- err=SCSIGet(); /* take the SCSI bus under control */
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid); /* select device to talk to */
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&reCmd,sizeof(reCmd)); /* send the command */
- ShowError("sending command");
- if (err != 0) goto error;
- readData(buffPtr,len); /* read the data */
- if (err != 0) goto error;
- myComplete(TIMEOUT); /* complete the operation */
- ShowError("completing read");
- error:;
- }
-
-
- write(lblock,buffPtr,len) /* writes a sector to disk */
- /* for other comments see read() above */
- long lblock;
- unsigned int len;
- char *buffPtr;
- {
- reWrCmd wrCmd;
-
- wrCmd.opCode=0xA;
- wrCmd.rsrv1=lblock>>16;
- wrCmd.addr=lblock&0xFFFF;
- wrCmd.length=1;
- wrCmd.rsrv2=0;
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&wrCmd,sizeof(wrCmd));
- ShowError("sending command");
- if (err != 0) goto error;
- writeData(buffPtr,len);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing write");
- error:;
- }
-
-
- readData(buffPtr, howMany) /* reads data from drive during data phase */
- Ptr buffPtr; /* pointer to store data */
- int howMany; /* number of bytes to read */
- {
- myTIB[0].scOpcode=scNoInc; /* read without increasing pointer */
- myTIB[0].scParam1= (long) buffPtr;
- myTIB[0].scParam2=howMany;
- myTIB[1].scOpcode=scStop; /* stop reading */
- myTIB[1].scParam1=0;
- myTIB[1].scParam2=0;
- #ifdef DEBUG
- printf("Reading data...\n");
- #endif
- if (BLIND) err=SCSIRBlind(myTIB); /* do the read */
- else err=SCSIRead(myTIB);
- #ifdef DEBUG
- printf("Data read !\n");
- #endif
- ShowError("reading data");
- }
-
-
- writeData(buffPtr, howMany) /* writes data to drive during data phase */
- /* for other comments see readData() above */
- char *buffPtr;
- int howMany;
- {
- myTIB[0].scOpcode=scNoInc;
- myTIB[0].scParam1= (long) buffPtr;
- myTIB[0].scParam2=howMany;
- myTIB[1].scOpcode=scStop;
- myTIB[1].scParam1=0;
- myTIB[1].scParam2=0;
- #ifdef DEBUG
- printf("Writing data...\n");
- #endif
- if (BLIND) err=SCSIWBlind(myTIB);
- else err=SCSIWrite(myTIB);
- #ifdef DEBUG
- printf("Data written, error = %d\n",err);
- #endif
- ShowError("writing data");
- }
-
-
- modSel(buff) /* send a Mode Select Command */
- /* this command can change operating parameters in the drive */
- Ptr buff; /* pointer to parameters */
- {
-
- reWrCmd mdSlCmd;
-
- mdSlCmd.opCode=0x15; /* opcode for the command */
- mdSlCmd.rsrv1=0;
- mdSlCmd.addr=0;
- mdSlCmd.length=12; /* send 12 bytes */
- mdSlCmd.rsrv2=0;
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&mdSlCmd,sizeof(mdSlCmd));
- ShowError("sending mode select command");
- if (err != 0) goto error;
- writeData(buff,12);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing mode select");
- error:;
- }
-
-
-
- reqSen(buff) /* send a Request Sense command */
- /* gets diagnostic error code from drive */
- Ptr buff;
- {
- reWrCmd rqSCmd;
-
- rqSCmd.opCode=3; /* opcode for the command */
- rqSCmd.rsrv1=0;
- rqSCmd.addr=0;
- rqSCmd.length=22; /* read 22 bytes of sense data */
- rqSCmd.rsrv2=0;
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&rqSCmd,sizeof(rqSCmd));
- ShowError("sending reqSense command");
- if (err != 0) goto error;
- readData(buff,22);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing reqSense");
- error:;
- }
-
-
- modeSen(buff,page,len) /* send a Mode Sense command */
- /* with this command the drive reports its device parameters */
- Ptr buff; /* pointer to receive the data */
- unsigned char page; /* number of parameter page to ask for */
- unsigned int len; /* number of bytes to read */
- {
- modeSenseCmd MSCmd;
-
- MSCmd.opCode=0x1A;
- MSCmd.LUN=0;
- MSCmd.rsrv1=0;
- MSCmd.PCF=0;
- MSCmd.pageCode=page;
- MSCmd.rsrv2=0;
- MSCmd.allocLen=len;
- MSCmd.rsrv3=0;
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&MSCmd,sizeof(MSCmd));
- ShowError("sending modeSen command");
- if (err != 0) goto error;
- readData(buff,len);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing modeSen");
- error:;
- }
-
-
- inquiry(buff,len) /* send an Inquiry command */
- /* with this command the drive reports its non-changeable parameters */
- Ptr buff; /* pointer to receive the data */
- unsigned int len; /* number of bytes to read */
- {
- modeSenseCmd inqCmd;
-
- inqCmd.opCode=0x12;
- inqCmd.LUN=0;
- inqCmd.rsrv1=0;
- inqCmd.PCF=0;
- inqCmd.pageCode=0;
- inqCmd.rsrv2=0;
- inqCmd.allocLen=len;
- inqCmd.rsrv3=0;
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&inqCmd,sizeof(inqCmd));
- ShowError("sending inquiry command");
- if (err != 0) goto error;
- readData(buff,len);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing inquiry");
- error:;
- }
-
-
- ReadDefect(buff,len) /* reads the defect sector list from drive */
- Ptr buff; /* pointer to receive the data */
- unsigned int len; /* number of bytes to read */
- {
- readDefCmd RDCmd;
- unsigned long actLen; /* actual length of defects list */
-
- Zero(&RDCmd,sizeof(RDCmd));
- RDCmd.opCode=0x37;
- RDCmd.which=0x18; /* send manufacturer's and user defined defects */
- RDCmd.allocLen=4; /* send only the list header first */
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&RDCmd,sizeof(RDCmd));
- ShowError("sending read defect command");
- if (err != 0) goto error;
- readData(&actLen,4);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing read defect");
- if (err != 0) goto error;
-
- actLen &= 0x0000FFFF; /* mask out the flags */
- actLen += 4; /* and now we have the right length of the defects list */
- if (len > actLen) len = actLen;
- RDCmd.allocLen=len & 0x00FF; /* this time read all of the defects list */
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&RDCmd,sizeof(RDCmd));
- ShowError("sending read defect command");
- if (err != 0) goto error;
- readData(buff,len);
- if (err != 0) goto error;
- myComplete(TIMEOUT);
- ShowError("completing read defect");
- error:;
- }
-
-
- Format() /* formats the disk */
- {
- fmtCmd FCmd;
- unsigned int iL; /* interleave value */
- char ans[20];
-
- printf("Do you really want to format SCSI ID = %d [y/n] ? ",SCSIid);
- scanf("%s",ans);
- if (ans[0] != 'y' && ans[0] != 'Y') return;
- printf("Suggested interleave values :\n");
- printf("4 for Mac Plus\n");
- printf("3 for Mac SE\n");
- printf("1 for all 68020/68030 models\n");
- printf("Enter interleave value : ");
- scanf("%d",&iL);
- FCmd.opCode=0x4;
- FCmd.pars=0x00; /* default format - retain previously defined defects */
- FCmd.rsrv1=0;
- FCmd.iLeave=iL&0xFF;
- FCmd.rsrv2=0;
- err=SCSIGet();
- ShowError("getting");
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- ShowError("selecting");
- if (err != 0) goto error;
- err=SCSICmd(&FCmd,sizeof(FCmd));
- ShowError("sending format command");
- if (err != 0) goto error;
- printf("Now formatting, please wait...\n");
- myComplete(LONGWAIT);
- printf("Formatting ended !\n");
- ShowError("completing format");
- error:;
- }
-
-
-
- long Capacity() /* returns the capacity of the drive in sectors */
- {
- modeSenseCmd MSCmd;
-
- Zero(&FPars,sizeof(FPars));
- MSCmd.opCode=0x1A; /* mode sense command */
- MSCmd.LUN=0;
- MSCmd.rsrv1=0;
- MSCmd.PCF=0;
- MSCmd.pageCode=3; /* ask for page three */
- MSCmd.rsrv2=0;
- MSCmd.allocLen=sizeof(FPars); /* no of bytes to ask */
- MSCmd.rsrv3=0;
- err=SCSIGet();
- if (err != 0) goto error;
- err=SCSISelect(SCSIid);
- if (err != 0) goto error;
- err=SCSICmd(&MSCmd,sizeof(MSCmd));
- if (err != 0) goto error;
- readData(&FPars,sizeof(FPars));
- myComplete(TIMEOUT);
- error:;
- return(FPars.blD.nBlocks); /* return the size, or null if error occured */
- }
-